import { _decorator, Component, director, Node } from 'cc';
const { ccclass, property } = _decorator;

import { socketServerConfig } from "../config/ServerConfig";
import Store from "../store/Store";
import NetMgr from "../net/NetMgr";
import RandomUtil from '../utils/RandomUtil';

/**挂在登录UI上 */
@ccclass('LoginMgr')
export default class LoginMgr extends Component {
    
    start() {
        
        NetMgr.init(socketServerConfig);//不走网络
    }

    /**
     * 登录按钮点击事件
     */
    async onLoginGuestBtnClickEvent() {
        console.log('onLoginGuestBtnClickEvent')
        // 登录
        let user = await NetMgr.loginByGuest();//不走网络

        // let user = this.createUserData();//随机模拟生成user数据

        console.log('user:', user)
        // 全局保存用户信息
        Store.user = user
        // 连接socket...
        console.log('NetMgr.connect 前')
        await NetMgr.connect(Store.user.id);//不走网络
        console.log('NetMgr.connect 后')
        console.log('Store.user.id='+ Store.user.id)


        const { room, index } = await NetMgr.joinRoom(Store.user)
        console.log('LoginMgrtMgr.room:-----')
        console.log(room)
        console.log(index)

        // 场景跳转
        director.loadScene('hall')
    }

    /**
     * 随机模拟生成user数据
     * 名称      类型        描述
     * id	    string	    用户id
     * name	    string	    用户名/昵称
     * balance	number	    余额
     */
    createUserData(){

        var userObj = { 
            id:  RandomUtil.makeString(5), 
            name: RandomUtil.makeString(5),
            balance:1000,
         }; 

        return userObj;
    }

}


